import { createRouter, createWebHashHistory } from "vue-router";
 
const routes = [
  {
    path: '/',
    redirect: '/tab'
  },
  {
    path: "/tab",
    component: () => import("@/views/TabView.vue"),
    name: "tab",
  },
  {
    path: "/user",
    component: () => import("@/views/UsersView.vue"),
    name: "user",
  },
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
    name: "login",
  },
  {
    path: "/designer",
    component: () => import("@/components/ReportDesignerComponent.vue"),
    name: "designer",
  },
  {
    path: "/viewer",
    component: () => import("@/components/WebDocumentViewer.vue"),
    name: "viewer",
  },
  {
    path: "/nursework",
    component: () => import("@/views/HomeView.vue"),
    name: "nursework",
  },
  {
    path: "/rtmsrecord",
    component: () => import("@/views/PatRtmsView.vue"),
    name: "rtmsrecord",
  }
];

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
});
 
router.beforeEach((to, from, next) => {
  let isAuthenticated = false 
  let CurUser="123";
  try
  {
    CurUser=  localStorage.getItem('CurUser');  
  }catch(e)
  {
  }
 
  console.log(CurUser)
 if(CurUser)   
    isAuthenticated=true
  // 这里应该是一个检查用户是否已经登录的逻辑
  //to.path === '/login' &&
  if (to.path === '/login' || isAuthenticated) {
    next() // 否则，正常导航到目标路由
  } else {
    next('/login'+'?redirect='+to.path) // 如果用户已登录，则重定向到首页
  }
})

export default router;
